﻿package com.athena.process
{
	import com.athena.controller.FriendItemController;
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;
	import com.adobe.serialization.json.JSON;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author ...
	 */
	public class FriendFunction 
	{
		public static var arrFriendGame : Array ;
		public static var arrFriendZing : Array ;
		public static var arrFriendTop : Array ;
		public static var FriendGame_List : MovieClip = new MovieClip() ;
		public static var FriendZing_List : MovieClip = new MovieClip() ;
		public static var FriendTop_List : MovieClip = new MovieClip() ;
		public static var scrFriendGame : ScrollPane = new ScrollPane() ; 
		public static var scrFriendZing : ScrollPane = new ScrollPane() ; 
		public static var scrFriendTop : ScrollPane = new ScrollPane() ; 
		
		public static function FriendGame_Click(evt : MouseEvent) {
			arrFriendGame = new Array() ;
			var obj : Object = new Object() ;
			obj.evt = "11" ;
			obj.T = "1" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj));
			//Cocaro.tf.appendText("==>SendFriend:" + JSON.encode(obj)) ;
			if (Cocaro.mainStage_mc.Friend_mc.BG_Game.numChildren > 1) {
				Cocaro.mainStage_mc.Friend_mc.BG_Game.removeChildAt(1) ;
			}			
			scrFriendGame.source = FriendGame_List; 
			scrFriendGame.setSize(180, 290); 
			scrFriendGame.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrFriendGame.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrFriendGame.move(0, 0);			
			Cocaro.mainStage_mc.Friend_mc.BG_Game.addChild(scrFriendGame) ;
			Cocaro.mainStage_mc.Friend_mc.FriendGame_btn.gotoAndStop(2) ;
			Cocaro.mainStage_mc.Friend_mc.FriendZing_btn.gotoAndStop(1) ;
			Cocaro.mainStage_mc.Friend_mc.FriendTop_btn.gotoAndStop(1) ;			
		}
		public static function FriendZing_Click(evt : MouseEvent) {
			var obj : Object = new Object() ;
			obj.evt = "11" ;
			obj.T = "2" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj));
			if (Cocaro.mainStage_mc.Friend_mc.BG_Zing.numChildren > 1) {
				Cocaro.mainStage_mc.Friend_mc.BG_Zing.removeChildAt(1) ;
			}			
			scrFriendZing.source = FriendZing_List; 
			scrFriendZing.setSize(180, 290); 
			scrFriendZing.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrFriendZing.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrFriendZing.move(0, 0);			
			Cocaro.mainStage_mc.Friend_mc.BG_Game.addChild(scrFriendZing) ;
			Cocaro.mainStage_mc.Friend_mc.FriendGame_btn.gotoAndStop(1) ;
			Cocaro.mainStage_mc.Friend_mc.FriendZing_btn.gotoAndStop(2) ;
			Cocaro.mainStage_mc.Friend_mc.FriendTop_btn.gotoAndStop(1) ;
		}
		public static function FriendTop_Click(evt : MouseEvent) {
			var obj : Object = new Object() ;
			obj.evt = "11" ;
			obj.T = "4" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj));
			Cocaro.mainSocket.sendService(JSON.encode(obj));
			if (Cocaro.mainStage_mc.Friend_mc.BG_Top.numChildren > 1) {
				Cocaro.mainStage_mc.Friend_mc.BG_Top.removeChildAt(1) ;
			}			
			scrFriendTop.source = FriendZing_List; 
			scrFriendTop.setSize(180, 290); 
			scrFriendTop.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrFriendTop.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrFriendTop.move(0, 0);			
			Cocaro.mainStage_mc.Friend_mc.FriendGame_btn.gotoAndStop(1) ;
			Cocaro.mainStage_mc.Friend_mc.FriendZing_btn.gotoAndStop(1) ;
			Cocaro.mainStage_mc.Friend_mc.FriendTop_btn.gotoAndStop(2) ;
		}
		public static function ReceiveFriendGame(str : String) {
			//Cocaro.tf.appendText("Game:" + str) ;
			var arrPlayer : Array = JSON.decode(str) as Array;
			//object.sortOn(["M"], [Array.NUMERIC]);
			arrPlayer.sortOn(["S"], [Array.NUMERIC]);
			for (var i:int = 0; i < arrPlayer.length; i++) 
			{
				//Cocaro.tf.appendText("==>N:" + arrPlayer[i].N) ;
				var obj : Object = new Object() ;
				obj.N = arrPlayer[i].N ;
				obj.S = arrPlayer[i].S ;
				var mcFriendItem : MovieClip = MovieClip(new Friend_Item()) ;
				mcFriendItem.name = "FriendGame" ;
				mcFriendItem.x = 3 ;
				mcFriendItem.y = arrFriendGame.length * 38 ;
				mcFriendItem.Name_txt.text = obj.N ;
				var formatText = new TextFormat() ;				
				formatText.font = "Arial" ;
				formatText.bold = true ;
				formatText.size = 12 ;
				formatText.color = 0xFFFFFF ;
				if (obj.S == false)
					formatText.color = 0x999999 ;
				mcFriendItem.Name_txt.setTextFormat(formatText) ;
				new FriendItemController(mcFriendItem, obj) ;
				FriendGame_List.addChild(mcFriendItem) ;
				arrFriendGame.push(obj) ;				
			}
			scrFriendGame.update() ;
		}
	}
	
}